package cn.lotusnut.rabbitmqboot.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author JiangMing Zhou
 * @Date 2022/12/1 16:00
 * @Version 1.8
 * 死信交换机配置
 */
@Configuration
public class DeadLetterConfiguration {

    /**
     * 业务交换机及队列
     * @return
     */
    @Bean
    public Queue businessQueue(){
        //设置消息队列属性
        Map<String,Object> param = new HashMap<>();
        param.put("x-message-ttl",5000);//设置队列消息过期时间
        param.put("x-dead-letter-exchange","dead_exchange"); //设置该队列的死信交换机
        param.put("x-dead-letter-routing-key","dead"); //指定死信路由
        return new Queue("business_queue",false,false,false,param);
    }

    @Bean
    public DirectExchange businessDirectExchange(){
        return new DirectExchange("business_exchange");
    }

    @Bean
    public Binding bindingBusinessQueueToBusinessDirectExchange(Queue businessQueue,DirectExchange businessDirectExchange){
        return BindingBuilder.bind(businessQueue).to(businessDirectExchange).with("business");
    }

    /**
     * 死信交换机、队列
     */
    @Bean
    public Queue deadQueue(){
        return new Queue("dead_queue");
    }

    @Bean
    public DirectExchange deadDirectExchange(){
        return new DirectExchange("dead_exchange");
    }

    @Bean
    public Binding bindingDeadQueueToDeadDirectExchange(Queue deadQueue,DirectExchange deadDirectExchange){
        return BindingBuilder.bind(deadQueue).to(deadDirectExchange).with("dead");
    }

}
